package com.jfs.qy.chain.component;

import com.jfs.qy.chain.AbstractServerStartHandler;
import com.jfs.qy.config.component.SystemInfoTask;
import com.jfs.qy.config.component.TaskExecutorReactor;
import com.jfs.qy.entity.response.SearchSystemInfoResponse;
import com.jfs.qy.enums.NvrApiEnum;
import com.jfs.qy.service.LogoutService;
import com.jfs.qy.service.SystemInfoService;
import com.jfs.qy.session.NvrSessionStorage;
import com.jfs.qy.utils.SystemCommonUtils;
import org.springframework.beans.factory.annotation.Qualifier;
import org.springframework.context.annotation.Lazy;
import org.springframework.http.HttpEntity;
import org.springframework.http.HttpMethod;
import org.springframework.http.HttpStatus;
import org.springframework.http.ResponseEntity;
import org.springframework.stereotype.Component;
import org.springframework.web.client.RestTemplate;

import java.net.URISyntaxException;
import java.security.KeyManagementException;
import java.security.KeyStoreException;
import java.security.NoSuchAlgorithmException;
import java.util.concurrent.CountDownLatch;

/**
 * @Description 查询系统详情组件（保持回话不会失效）
 * @E-mail cwm3412638@163.com
 * @Author cwm
 * @Date 2022/11/4
 */
@Component(value = SystemInfoComponent.BEAN_NAME)
public class SystemInfoComponent extends AbstractServerStartHandler implements SystemInfoService {

    public final static String BEAN_NAME = "systemInfoComponent";
    /**
     * 下一个责任链对象
     *
     * @param abstractServerStartHandler
     */
    private final AbstractServerStartHandler abstractServerStartHandler;

    private final SystemCommonUtils systemCommonUtils;

    private final RestTemplate restTemplate;
    private final TaskExecutorReactor taskExecutorReactor;
     private final LoginComponent loginComponent;

    public SystemInfoComponent(@Qualifier(value = SystemIpcListInfoComponent.BEAN_NAME)
                               AbstractServerStartHandler abstractServerStartHandler,
                               SystemCommonUtils systemCommonUtils,
                               RestTemplate restTemplate,
                               TaskExecutorReactor taskExecutorReactor,
                               @Lazy LoginComponent loginComponent) {
        this.abstractServerStartHandler = abstractServerStartHandler;
        this.systemCommonUtils = systemCommonUtils;
        this.restTemplate = restTemplate;
        this.taskExecutorReactor=taskExecutorReactor;
        this.loginComponent=loginComponent;
    }

    @Override
    public void handlerOperation() throws URISyntaxException, NoSuchAlgorithmException, KeyStoreException, KeyManagementException {
        //退出登录接口实现类
        LogoutService logoutService = () -> {
            final HttpEntity<?> entity = new HttpEntity<>(null, NvrSessionStorage.headers);
            String requestUrl = systemCommonUtils.getRequestUrl(NvrApiEnum.LOGOUT);
            return restTemplate.exchange(requestUrl, HttpMethod.POST, entity, String.class);
        };
        //开启查询系统详情任务
        SystemInfoTask task = new SystemInfoTask(this,logoutService,loginComponent);
        taskExecutorReactor.schedule(task,0, systemCommonUtils.getSystemInfoInterval());
        //调用下一个责任链
        abstractServerStartHandler.handlerOperation();
    }

    @Override
    public SearchSystemInfoResponse searchSystemInfo() {
        //获取请求Url
        String requestUrl = systemCommonUtils.getRequestUrl(NvrApiEnum.SYSTEM_INFO);
        final HttpEntity<?> entity = new HttpEntity<>(null, NvrSessionStorage.headers);
        ResponseEntity<SearchSystemInfoResponse> exchange = restTemplate.exchange(requestUrl, HttpMethod.POST, entity, SearchSystemInfoResponse.class);
        if (HttpStatus.OK.equals(exchange.getStatusCode())) {
            return exchange.getBody();
        }
        return null;
    }

}
